<template>
	<view>
		<view class="row center space-between dym" style="margin-top: 40rpx;">
			<view class="row center">
				<view class="row center">
					<image class="dymt" src="/static/pay/icon_16.png"></image>
				</view>
				<view v-if="dymzt == 1" class="zi">大姨妈走了</view>
				<view v-else class="zi">大姨妈来了</view>
			</view>
			<view>
				<u-switch size="45" active-color="#643A92" @change="checkeddymf" v-model="checkeddym"></u-switch>
			</view>
		</view>

		<view class="row center space-between dym">
			<view class="row center">
				<view class="row center">
					<image class="dymt" src="/static/pay/icon_17.png"></image>
				</view>
				<view class="zi">同房</view>
			</view>
			<view>
				<u-switch @change="checkedtmf" size="45" active-color="#643A92" v-model="checkedtm"></u-switch>
			</view>
		</view>

		<view v-if="aalog" class="row center space-between dym"
			@click="navTo(`/pages/fetation/components/aalist?nowtiem=${newClickTime}`)">
			<view class="row center">
				<view class="row center">
					<image class="dymt" src="/static/pay/icon_24.png"></image>
				</view>
				<view class="zi">同房记录</view>
			</view>

			<view class="row center">
				<!-- <view class="tfsj">第一次1时，第二次13时...</view> -->

				<view class="tfsj">{{ tf_record_title }}</view>

				<image class="dymt" src="/static/pay/icon_22.png"></image>
				<view class="jantou">></view>
			</view>
		</view>

		<view @click="twdj" class="row center space-between dym">
			<view class="row center">
				<view class="row center">
					<image class="dymt" src="/static/pay/icon_19.png"></image>
				</view>
				<view class="zi">体温</view>
			</view>
			<view class="row center">
				<view v-if="twwz" class="twwzsd">{{ twwz }}℃</view>
				<image v-else class="didi" src="/static/pay/icon_18.png"></image>
			</view>
		</view>

		<view @click="tzdj" class="row center space-between dym">
			<view class="row center">
				<view class="row center">
					<image class="dymt" src="/static/pay/icon_20.png"></image>
				</view>
				<view class="zi">体重</view>
			</view>
			<view>
				<view v-if="gjwz" class="twwzsd">{{ gjwz }}Kg</view>
				<image v-else class="didi" src="/static/pay/icon_18.png"></image>
			</view>
		</view>

		<u-picker title="添加同房记录" @confirm="queren" mode="selector" v-model="tfshow" :default-selector="[8]"
			:range="selector"></u-picker>
		<u-keyboard @backspace="backspace" :type="type" :twwz="twwz" @change="twChange" @confirm="twhd" ref="uKeyboard"
			tips="体温" mode="number" v-model="twshow"></u-keyboard>

		<u-keyboard @backspace="backspace" :type="type" :gjwz="gjwz" @change="valChange" @confirm="tzhdz"
			ref="uKeyboard" tips="体重" mode="number" v-model="tzzshow"></u-keyboard>
	</view>
</template>
<script>
	import api from '@/api/fetation.js';
	export default {
		props: {
			ymm: '',
			clicktime: {
				type: [Number, String],
				default: '1950-01-01'
			},
			today: {},
			dymzt: '', //状态 2来了-关 1走了-开
			tfal: '', //判断同房按钮开关 1有数据开起  2没有数据关
		},
		watch: {
			//ym 1是开起 2关闭
			ymm: function(newData) {

				this.ym = newData

			},
			tfal: function(newData) {
				if (newData == 1) {
					this.checkedtm = true
					this.aalog = true
				} else {
					this.checkedtm = false
					this.aalog = false
				}
			},
			dymzt: function(newData) {
				if (newData == 1) {
					this.checkeddym = true;
				} else {
					this.checkeddym = false;
				}

				deep: true;
			},
			clicktime: function(newData) {
				// console.log('bot点击时间111',newData)
				console.log('bot点击时间222', this.today)
				var dday = this.today


				console.log('bot点击时间333', dday)
				if (dday) {
					if (dday.temperature) {
						this.twwz = dday.temperature
					} else {
						this.twwz = ""
					}
					if (dday.weight) {
						this.gjwz = dday.weight
					} else {
						this.gjwz = ""
					}
				} else {
					this.twwz = ""
					this.gjwz = ""
				}


				this.newClickTime = newData;
			},
			today: function(newData) {
				console.log('监听后台选择日期当天数据bot', newData);
				if (newData) {
					// if(newData.temperature){
					// this.twwz = newData.temperature	
					// }else{
					// 	this.twwz = ""
					// }
					// if(newData.weight){
					// 	this.gjwz = newData.weight
					// }else{
					// 	this.gjwz =""
					// }
						
						
					this.twwz = newData.temperature;
					this.gjwz = newData.weight;
					this.tf_record = newData.tf_record;
					if (newData.tf_record_title) {
						this.checkedtm = true
					} else {
						this.checkedtm = false
					}
					if (newData.tf_record_title) {
						this.tf_record_title = newData.tf_record_title;
						this.checkedtm = true
						this.aalog = true
						
						
					} else {
						
						this.tf_record_title = '';
					}
				} else {
					this.twwz = '';
					this.gjwz = '';
					this.tf_record = '';
					this.tf_record_title = '';
				}
				
				deep: true;
				
			}
		},
		data() {
			return {
				ym: '',
				// dymztnew:'',//状态 1来了 2走了
				newClickTime: '', //先中时间
				aalog: true,
				tf_record: '', //提交同房记录选择的时间 17时
				tf_record_title: '',
				twshow: false,
				twwz: 0,
				gjwz: 0,
				selector: [
					'0时',
					'1时',
					'2时',
					'3时',
					'4时',
					'5时',
					'6时',
					'7时',
					'8时',
					'9时',
					'10时',
					'11时',
					'12时',
					'13时',
					'14时',
					'15时',
					'16时',
					'17时',
					'18时',
					'19时',
					'20时',
					'21时',
					'22时',
					'23时'
				],
				tfshow: false,
				tzzshow: false, //体温
				checkedtm: true,
				checkeddym: false,
				type: '', //1温度  2体重
				openid: ''
			};
		},

		methods: {
			backspace() {
				// 删除value的最后一个字符
				if (this.type == 1) {
					if (this.twwz.length) this.twwz = this.twwz.substr(0, this.twwz.length - 1);
				} else {
					if (this.gjwz.length) this.gjwz = this.gjwz.substr(0, this.gjwz.length - 1);
				}
			},

			// 提交同房记录
			async submitRecord() {
				var openid = uni.getStorageSync('openid');
				var arr = {
					openid: openid,
					time: this.newClickTime,
					tf_record: this.tf_record
				};

				const {
					code
				} = await api.submitRecord(arr);

				return code;
			},

			twdj() {
				this.twshow = true;

				this.type = 1;
			},
			twChange(val) {
				// 将每次按键的值拼接到value变量中，注意+=写法

				if (this.twwz == 0) {
					this.twwz = val;
				} else {
					this.twwz = `${this.twwz}${val}`;
				}
			},
			checkeddymf(status) {
				this.$emit('great', status);
			},
			async checkedtmf(status) {
				console.log('同房按钮切换', status)
				this.aalog = status;
				if (status == true) {
					this.tfshow = true;
					var tf = 1
				} else {
					this.tfshow = false;
					var tf = 2

				}

				var arr = [{
					temperature: '',
					time: this.newClickTime,
					tf_record: '',
					weight: '',
					ym: this.ym,
					tf: tf
				}];
				let dataArr = JSON.stringify(arr);
				var openid = uni.getStorageSync('openid');
				let tem = {
					openid: openid,
					data: dataArr
				};

				// const {
				// 	code
				// } = await api.submitRedayRecord(tem);
			},
			tzdj() {
				this.type = 2;
				this.tzzshow = true;
			},
			//增加同房记录
			async queren(e) {
				var _this = this;
				if (_this.tf_record) {
					_this.tf_record = `${_this.tf_record},${e[0]}`;
				} else {
					_this.tf_record = e[0];
				}

				var code = await _this.submitRecord();





				if (code == 1) {
					var arr = {
						openid: uni.getStorageSync('openid'),
						time: this.newClickTime
					};
					const {
						data
					} = await api.getList(arr);
					_this.tf_record_title = data.today.tf_record_title

					uni.showToast({
						title: '增加成功',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: '增加失败',
						duration: 2000
					});
				}
			},
			valChange(val) {
				// 将每次按键的值拼接到value变量中，注意+=写法

				if (this.gjwz == 0) {
					this.gjwz = val;
				} else {
					this.gjwz = `${this.gjwz}${val}`;
				}
			},

			////增加体重数据
			async tzhdz() {
				var arr = [{
					temperature: this.twwz,
					time: this.newClickTime,
					tf_record: '',
					weight: this.gjwz,
					ym: this.ym,
					tf: 2
				}];
				let dataArr = JSON.stringify(arr);
				var openid = uni.getStorageSync('openid');
				let tem = {
					openid: openid,
					data: dataArr
				};

				const {
					code
				} = await api.submitRedayRecord(tem);
				if (code == 1) {

					await api.getList({
						openid: this.openid,
						time: this.newClickTime
					})

					uni.showToast({
						title: '增加成功',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: '增加失败',
						duration: 2000
					});
				}
			},

			//增加体温数据
			async twhd() {
				var arr = [{
					temperature: this.twwz,
					time: this.newClickTime,
					tf_record: '',
					weight: this.gjwz,
					ym: this.ym,
					tf: 2
				}];
				let dataArr = JSON.stringify(arr);
				var openid = uni.getStorageSync('openid');
				let tem = {
					openid: openid,
					data: dataArr
				};

				const {
					code
				} = await api.submitRedayRecord(tem);
				if (code == 1) {
					await api.getList({
						openid: uni.getStorageSync('openid'),
						time: this.newClickTime
					})
					uni.showToast({
						title: '增加成功',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: '增加失败',
						duration: 2000
					});
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	.dym {
		padding-left: 30rpx;
		padding-right: 30rpx;
		margin-top: 30rpx;
		margin-bottom: 30rpx;

		.dymt {
			width: 26rpx;
			height: 26rpx;
		}

		.zi {
			padding-left: 10rpx;
		}

		.jantou {
			margin-left: 20rpx;
		}

		.tfsj {
			// width: 320rpx;
			height: 26rpx;
			font-size: 26rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #fb5235;
			line-height: 26rpx;
			text-align: center;
		}

		.didi {
			width: 42rpx;
			height: 8rpx;
		}

		.twwzsd {
			height: 21rpx;
			font-size: 26rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #fb5235;
			line-height: 26rpx;
		}
	}
</style>
